-module (add_two).
-export ([start/0, request/1, loop/0]).

start() ->
	process_flag(trap_exit, true),
	io:format("start add_two self : ~p~n", [self()]),
	Pid = spawn_link(add_two, loop, []),
	io:format("start add_two pid : ~p~n", [Pid]),
	register(add_two, Pid),
	{ok, Pid}.

request(Int) ->
	add_two ! {request, self(), Int},
	io:format("add_two request: ~p~n", [self()]),
	receive
		{result, Result} ->
			Result;
		{'EXIT', Pid, Reason} ->
			io:format("add_two request receive : ~p : ~p~n", [Pid, Reason]),
			{error, Reason}
	after 1000 ->
		timeout
	end.

loop() ->
	receive
		{request, Pid, Msg} ->
			io:format("add_two loop receive : ~p~n", [self()]),
			Pid ! {result, Msg+2}
	end,
	loop().







